//
//  WBRootViewController.m
//  SinaWeiBo
//
//  Created by lanou3g on 15/11/24.
//  Copyright © 2015年 阎海新. All rights reserved.
//

#import "WBRootViewController.h"
#import "WBHomeTableViewController.h"
#import "WBMessageCnterTableViewController.h"
#import "WBPublishTableViewController.h"
#import "WBDiscoverTableViewController.h"
#import "WBProfileTableViewController.h"

#define RGBColor(r, g, b) [UIColor colorWithRed:(r) / 255.0 green:(g)/ 255.0 blue:(b)/255.0 alpha: 1.0 ]

#define WBRandomColor RGBColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))

@interface WBRootViewController ()

@end

@implementation WBRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1、 初始化子控制器
    WBHomeTableViewController * homeVC = [[WBHomeTableViewController alloc] init];
    [self addChildVC:homeVC title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    WBMessageCnterTableViewController * messageCenterVC = [[WBMessageCnterTableViewController alloc] init];
    [self addChildVC:messageCenterVC title:@"消息" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    WBPublishTableViewController * publishVC = [[WBPublishTableViewController alloc] init];
    [self addChildVC:publishVC title:@"发表微博" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    WBDiscoverTableViewController * discoverVC = [[WBDiscoverTableViewController alloc] init];
    [self addChildVC:discoverVC title:@"发现" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    WBProfileTableViewController * profileVC = [[WBProfileTableViewController alloc] init];
    [self addChildVC:profileVC title:@"我" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    
}

/**
 *  添加一个子控制器
 *
 *  @param childVC       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void) addChildVC:(UIViewController *) childVC title:(NSString *) title image:(NSString *) image selectedImage:(NSString *) selectedImage {
    
    // 合并为
    childVC.title = title; // 可以同时设置tabbar和navBar的文字
//    childVC.tabBarItem.title = title;
//    childVC.navigationItem.title = title;
    
    //设置子控制器的图片
    childVC.tabBarItem.image = [UIImage imageNamed:image];
    // 声明: 这张图片按照原始的样子显示出来,不要自动渲染成其他颜色
    childVC.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
    // 设置文字的样式
    NSMutableDictionary * textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = RGBColor(123, 123, 123);
    
    NSMutableDictionary * selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [childVC.tabBarItem setTitleTextAttributes:textAttrs forState:(UIControlStateNormal)];
    [childVC.tabBarItem setTitleTextAttributes:selectTextAttrs forState:(UIControlStateSelected)];
    
    childVC.view.backgroundColor = WBRandomColor;
    
    // 先给外面传进来的小控制器包装一个导航控制器
    UINavigationController * rootNC = [[UINavigationController alloc] initWithRootViewController:childVC];
    // 添加为子控制器
    [self addChildViewController:rootNC];
    
}

@end
